/**
 * @file        os_task.c
 * @brief       XXXX
 * @note        XXXX
 * @author      靳普诏(puzhao.jin@hopechart.com)
 * @date        2025/09/15
 * @version     1.0

 * @par         修改日志
 * <table>
 * <tr><th>Date         <th>Version     <th>Author      <th> Description
 * <tr><td>2025/09/15   <td>1.0         <td>靳普诏       <td> 创建初始版本
 * @copyright 杭州鸿泉物联网技术股份有限公司
 */
 
#include "az_clib/os/os_task.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"


static void vTaskCode( void * pvParameters )
{
    TOsTask *self = (TOsTask *)pvParameters;
    TOsTaskOnExecMethod on_exec;

    self->state_ = kOsTaskStateIdle;

    while (self->state_ != kOsTaskStateDestroy)
    {
        if (self->state_ == kOsTaskStateIdle)
        {
            if (self->req_start_)
            {
                self->state_ = kOsTaskStateStarting;
                self->req_start_ = False;
            }
            else
                vTaskDelay(50);
        }
        else if (self->state_ == kOsTaskStateStarting)
        {
            on_exec = self->params.on_start_method;
            if (on_exec)
                on_exec(self);
            self->state_ = kOsTaskStateRunning;
        }
        else if (self->state_ == kOsTaskStateRunning)
        {
            on_exec = self->params.on_run_method;
            if (on_exec != NULL)
                on_exec(self);

            if (self->req_stop_)
            {
                self->state_ = kOsTaskStateStopping;
                self->req_stop_ = False;
            }
            else
                vTaskDelay(50);
        }
        else if (self->state_ == kOsTaskStateStopping)
        {
            on_exec = self->params.on_stop_method;
            if (on_exec != NULL)
                on_exec(self);

            self->state_ = kOsTaskStateIdle;
        }
        else
            self->state_ = kOsTaskStateIdle;
    }
}



//=======================================================================================================================

static UBaseType_t L_OsTaskPriorityToFreeRtos(TOsTaskPriority priority)
{
    UBaseType_t freeRtosPriority = tskIDLE_PRIORITY;

    if (priority >= kOsTaskPriorityHigh)
        freeRtosPriority = (kOsTaskPriorityHigh | portPRIVILEGE_BIT);
    else 
        freeRtosPriority = (priority | portPRIVILEGE_BIT);

    return freeRtosPriority;
}



Int32 TOsTaskCreate(TOsTask *self, const TOsTaskParam *params)
{
    Int32 result = 0;

    if (self != NULL && params != NULL)
    {
        TaskHandle_t xHandle = NULL;
        UBaseType_t task_priority = L_OsTaskPriorityToFreeRtos(params->priority);

        self->req_stop_ = False;
        self->req_start_ = False;
        self->state_ = kOsTaskStateCreate;
        self->params = *params;

        xTaskCreate( vTaskCode, params->task_name, params->stack_size, self, task_priority, &xHandle );

        if (xHandle != NULL)
        {
            self->req_destroy_ = False;
            self->state_ = kOsTaskStateIdle;
            self->task_handle_ = xHandle;

            vTaskResume( xHandle );
        }
        else
        {
            self->state_ = kOsTaskStateDestroy;
            result = -1;
        }
    }

    return result;
}


void TOsTaskDestroy(TOsTask *self)
{
    if (self->task_handle_ != NULL)
    {
        self->req_destroy_ = True;
        TOsTaskStop(self);

        while (self->state_ != kOsTaskStateDestroy)
        {
            self->req_stop_ = True;
            vTaskDelay(50);
        }

        vTaskResume(self->task_handle_);
        vTaskDelete(self->task_handle_);
        
        self->task_handle_ = NULL;
        self->req_destroy_ = False;
    }
}

Int32 TOsTaskStart(TOsTask *self)
{
    Int32 result = 0;

    if (self != NULL && self->task_handle_ != NULL)
    {
        self->req_start_ = True;
    }

    return result;
}

Int32 TOsTaskStop(TOsTask *self)
{
    Int32 result = 0;

    if (self != NULL && self->task_handle_ != NULL)
    {
        self->req_stop_ = True;
    }

    return result;
}

Bool TOsTaskIsRunning(TOsTask *self)
{
    return self != NULL && (self->state_ == kOsTaskStateRunning || self->state_ == kOsTaskStateStarting);
}



Bool TOsTaskReqStop(TOsTask *self)
{
    return self != NULL && self->req_stop_;
}



///< Generated on "2025-09-15 22:37:51" by the tool "gen_hq_file.py >> V20231119" 

